package model;

import persistency.writer.Writable;
import util.Date;

/**
 * Registration.java
 * Purpose: this class represents a registration to a course offered by the school
 * 
 * @author Hans Brems
 * @version 1.0 03/11/2011
 */

public class Registration implements Writable, Cloneable, Comparable<Registration> {

	private OfferedCourse offeredCourse;
	private Date registrationDate; 
	private Student student;
	private int primaryKey;
	private static int registrationCounter = 0;
	
	/** Constructs a new registration object */
	public Registration() {
		primaryKey = registrationCounter++;
	}

	/** Constructs a registration object */
	public Registration(OfferedCourse offeredCourse, Date registrationDate, Student student) {
		this.offeredCourse = offeredCourse;
		this.registrationDate = registrationDate;
		this.student = student;
		primaryKey = registrationCounter++;
	}
	
	/** Returns the course which is being offered **/
	public OfferedCourse getOfferedCourse() {
		return offeredCourse;
	}

	/** Sets the course which is being offered **/
	public void setOfferedCourse(OfferedCourse offeredCourse) {
		this.offeredCourse = offeredCourse;
	}

	/** Returns the student which is being registered for the course **/
	public Student getStudent() {
		return student;
	}

	/** Sets the student which is being registered for the course **/
	public void setStudent(Student student) {
		this.student = student;
	}

	/** Returns the registrationDate **/
	public Date getRegistrationDate() {
		return registrationDate;
	}

	/** Sets the date of the registration **/
	public void setRegistrationDate(Date registrationDate) {
		this.registrationDate = registrationDate;
	}
	
	/** Checks for equality with another object **/
	@Override
	public boolean equals(Object object) {
		
		Registration otherRegistration;
		
		// Check for same reference
		if (this == object) {
			return true;
		}
		
		// Check if object type is equal
		if (object == null || object.getClass() != this.getClass()) {
			return false;
		}
		
		// Cast object to correct class
		otherRegistration = (Registration)object;
		
		// Check if object properties are equal
		return otherRegistration.getPrimaryKey().equals(getPrimaryKey());
	}
	
	
	
	/** Returns a string representation of the object **/
	@Override
	public String toString() {
		return String.format("%s (%s)", (student != null)?student.getName(): "" , this.registrationDate);
	}

	/** Clones the object **/
	@Override
	public Object clone() throws CloneNotSupportedException {
		
		// Copy parent
		Registration clone = (Registration)super.clone();
		
		// Set properties
		clone.offeredCourse = this.offeredCourse;
		clone.registrationDate = this.registrationDate;
		clone.student = this.student;
		
		// Return result
		return clone;
	}	

	/** Compares the object to another object **/
	public int compareTo(Registration registration) {
		return this.toString().compareTo(registration.toString());
	}

	@Override
	public String toDataString() {
		return student.getStudentNumber() + "\t" + registrationDate.getDateEuropeanFormat() + "\n";
	}
	@Override
	public int hashCode(){
		return primaryKey;
	}
	
	@Override
	public Object getPrimaryKey() {
		return primaryKey;
	}

	public void setPrimaryKey(int primaryKey) {
		this.primaryKey = primaryKey;
	}
	
	
}